package com.zhxt.system.service.impl;

import com.zhxt.common.utils.StringUtils;
import com.zhxt.system.domain.LeaderImport;
import com.zhxt.system.domain.SysGw;
import com.zhxt.system.domain.SysTjcode;
import com.zhxt.system.domain.ZhxtLeader;
import com.zhxt.system.mapper.LeaderMapper;
import com.zhxt.system.service.ILeaderSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 管理干部 服务实现
 */

@Service
public class LeaderSerivceImpl implements ILeaderSerivce {

    @Autowired
    private LeaderMapper leaderMapper;
    /**
     * 查询数据最后一个月份
     */
    public String getMaxYearmon(){
        return leaderMapper.getMaxYearmon();
    }
    /**
     * 查询机构列表
     */
    public List<SysTjcode> getTjcodeList(String tjcode){
        String yearmon=leaderMapper.getMaxYearmon();
        if(StringUtils.isNotEmpty(yearmon)){
            if(tjcode.equals("41000099000a00")){
                return leaderMapper.getLeaderDeptList(tjcode,yearmon)==null?new ArrayList<>():leaderMapper.getLeaderDeptList(tjcode,yearmon);
            }else{
                return leaderMapper.getTjcodeList(tjcode,yearmon)==null?new ArrayList<>():leaderMapper.getTjcodeList(tjcode,yearmon);
            }
        }
        return new ArrayList<>();
    }

    /**
     * 查询机构列表--中支机构列表
     */
    public List<SysTjcode> getTjcodeZzList(){
        String yearmon=leaderMapper.getMaxYearmon();
        if(StringUtils.isNotEmpty(yearmon)){
           return leaderMapper.getTjcodeZzPmList(yearmon)==null?new ArrayList<>():leaderMapper.getTjcodeZzPmList(yearmon);
        }
        return new ArrayList<>();
    }

    /**
     * 查询机构名称
     */
    public SysTjcode getTjName(String tjcode){
        if(StringUtils.isNotEmpty(tjcode)){
            return leaderMapper.getTjName(tjcode)==null?new SysTjcode():leaderMapper.getTjName(tjcode);
        }
        return new SysTjcode();
    }
    /**
     * 机构、月份、姓名查询接口
     */
    public List<ZhxtLeader> getLeaderList(String yearmon,String tjcode,String empname){
        if(tjcode.isEmpty()){
            return leaderMapper.getLeaderList(yearmon,tjcode,empname,"0")==null?new ArrayList<>():leaderMapper.getLeaderList(yearmon,tjcode,empname,"0");
        }else if(tjcode.charAt(0)=='T'){
            return leaderMapper.getLeaderList(yearmon,tjcode,empname,"0")==null?new ArrayList<>():leaderMapper.getLeaderList(yearmon,tjcode,empname,"0");
        }else if(tjcode.charAt(0)=='4'){
            return leaderMapper.getLeaderList(yearmon,tjcode,empname,"2")==null?new ArrayList<>():leaderMapper.getLeaderList(yearmon,tjcode,empname,"2");
        }else{
            return leaderMapper.getLeaderList(yearmon,tjcode,empname,"1")==null?new ArrayList<>():leaderMapper.getLeaderList(yearmon,tjcode,empname,"1");
        }

    }

    /**
     * 获取岗位列表
     */
    public List<SysGw> getGwList(String gwname){
        if(StringUtils.isNotEmpty(gwname)){
            return leaderMapper.getGwList(gwname)==null?new ArrayList<>():leaderMapper.getGwList(gwname);
        }
        return new ArrayList<>();
    }
    /**
     * 机构、月份、岗位历任信息查询接口
     */
    public List<ZhxtLeader> getGwHistory(String tjcode,String gwname){
        return leaderMapper.getGwHistory(tjcode,gwname)==null?new ArrayList<>():leaderMapper.getGwHistory(tjcode,gwname);
    }

    public List<ZhxtLeader> getAllGwHistory(String tjcode){
        return leaderMapper.getAllGwHistory(tjcode)==null?new ArrayList<>():leaderMapper.getAllGwHistory(tjcode);
    }

    /**
     * 导入干部信息
     */
    public int importLeader(List<LeaderImport> leaderImportList, String yearmon){
        this.delLeader(yearmon);
        //循环插入数据
        int succNum=0;
        int errNum=0;
        for(LeaderImport leaderImport:leaderImportList){
            leaderImport.setYearmon(yearmon);
            int ii=this.insertOneLeader(leaderImport);
            if(ii > 0){
                succNum++;
            }else{
                errNum++;
            }
        }

        return succNum;
    }

    /**
     * 插入一个干部信息
     */
    public int insertOneLeader(LeaderImport leaderImport){
        return leaderMapper.insertOneLeader(leaderImport);
    }

    /**
     * 删除一个月数据
     */
    public int delLeader(String yearmon){
        return leaderMapper.delLeader(yearmon);
    }

    /**
     * 首页一把手列表信息
     */
    public List<ZhxtLeader> getLeaderIndex(String yearmon){
        return leaderMapper.getLeaderIndex(yearmon)==null?new ArrayList<>():leaderMapper.getLeaderIndex(yearmon);
    }
}
